import json
import os
from pathlib import Path
from sqlite3 import dbapi2

from fastapi import Request
from fastapi import Depends
from fastapi.responses import RedirectResponse
from nicegui import Client, app, ui
from sqlmodel import Session
from sqlmodel import Session
from starlette.middleware.base import BaseHTTPMiddleware

from auth_middleware import AuthMiddleware
from db.db import close_db, init_db
from login import login
from sections.admin.page_admin import page_admin
from sections.home.page_home import page_home
from sections.mobile.lte_freq_record_form import lte_freqrecordmmanage
from sections.mobile.mobile_home import mobile_home
from settings import ROOT, unrestricted_page_routes
from wechat.wechat import *

# 5.在应用启动时，初始化数据库（定义数据表，这里用的是sqlite，会自动创建db2.db文件）
app.on_startup(init_db)
# 6.在应用销毁时，关闭数据库连接
app.on_shutdown(close_db)
# 7.挂载“认证中间件”
app.add_middleware(AuthMiddleware)

ROOT = Path(__file__).parent
app.add_static_files("/assets", ROOT / "assets")
print(ROOT)


def fake_data():
    # 修改前
    # from settings import engine, Session
    # 修改后
    from db.db import engine, Session
    from db.fake_data import (
        fake_base_station,
        fake_lte_freq,
    )

    db2 = Session(engine)
    fake_base_station(db2)
    fake_lte_freq(db2)


# 生成一些假数据以测试
# fake_data()


ui.run(
    title="基站环测系统",
    storage_secret="gcmc_base_station",
    favicon=ROOT / "assets" / "favicon.png",
    viewport="width=device-width, initial-scale=1.0, maximum-scale=2.0, minimum-scale=0.5, user-scalable=yes",
    port=80,
    reload=True,
)
